package com.ruoyi.kanban.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.kanban.domain.KanbanOrder;
import com.ruoyi.kanban.service.IKanbanOrderService;

/**
 * 看板订单Controller
 * 
 * @author ruoyi
 * @date 2025-01-18
 */
@RestController
@RequestMapping("/kanban/order")
@Validated
public class KanbanOrderController extends BaseController {

    @Autowired
    private IKanbanOrderService kanbanOrderService;

    /**
     * 查询看板订单列表
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(KanbanOrder kanbanOrder,
                             @RequestParam(value = "beginCreateTime", required = false) String beginCreateTime,
                             @RequestParam(value = "endCreateTime", required = false) String endCreateTime,
                             @RequestParam(value = "beginRequiredDeliveryTime", required = false) String beginRequiredDeliveryTime,
                             @RequestParam(value = "endRequiredDeliveryTime", required = false) String endRequiredDeliveryTime) {
        startPage();
        
        // 将日期参数设置到params中
        if (beginCreateTime != null || endCreateTime != null || 
            beginRequiredDeliveryTime != null || endRequiredDeliveryTime != null) {
            if (kanbanOrder.getParams() == null) {
                kanbanOrder.setParams(new java.util.HashMap<>());
            }
            if (beginCreateTime != null) {
                kanbanOrder.getParams().put("beginCreateTime", beginCreateTime);
            }
            if (endCreateTime != null) {
                kanbanOrder.getParams().put("endCreateTime", endCreateTime);
            }
            if (beginRequiredDeliveryTime != null) {
                kanbanOrder.getParams().put("beginRequiredDeliveryTime", beginRequiredDeliveryTime);
            }
            if (endRequiredDeliveryTime != null) {
                kanbanOrder.getParams().put("endRequiredDeliveryTime", endRequiredDeliveryTime);
            }
        }
        
        List<KanbanOrder> list = kanbanOrderService.selectKanbanOrderList(kanbanOrder);
        return getDataTable(list);
    }

    /**
     * 导出看板订单列表
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:export')")
    @Log(title = "看板订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KanbanOrder kanbanOrder) {
        List<KanbanOrder> list = kanbanOrderService.selectKanbanOrderList(kanbanOrder);
        ExcelUtil<KanbanOrder> util = new ExcelUtil<KanbanOrder>(KanbanOrder.class);
        util.exportExcel(response, list, "看板订单数据");
    }

    /**
     * 获取看板订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(kanbanOrderService.selectKanbanOrderById(id));
    }

    /**
     * 根据看板编码获取看板订单信息
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:query')")
    @GetMapping(value = "/code/{kanbanCode}")
    public AjaxResult getInfoByCode(@PathVariable("kanbanCode") String kanbanCode) {
        return AjaxResult.success(kanbanOrderService.selectKanbanOrderByCode(kanbanCode));
    }

    /**
     * 新增看板订单
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:add')")
    @Log(title = "看板订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody KanbanOrder kanbanOrder) {
        return toAjax(kanbanOrderService.insertKanbanOrder(kanbanOrder));
    }

    /**
     * 修改看板订单
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:edit')")
    @Log(title = "看板订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody KanbanOrder kanbanOrder) {
        return toAjax(kanbanOrderService.updateKanbanOrder(kanbanOrder));
    }

    /**
     * 删除看板订单
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:remove')")
    @Log(title = "看板订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(kanbanOrderService.deleteKanbanOrderByIds(Arrays.stream(ids).collect(Collectors.toList())));
    }

    /**
     * 根据框架协议ID查询看板订单列表
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:list')")
    @GetMapping("/framework/{frameworkId}")
    public AjaxResult getOrdersByFrameworkId(@PathVariable("frameworkId") Long frameworkId) {
        List<KanbanOrder> list = kanbanOrderService.selectKanbanOrderByFrameworkId(frameworkId);
        return AjaxResult.success(list);
    }

    /**
     * 根据业务状态查询看板订单列表
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:list')")
    @GetMapping("/status/{businessStatus}")
    public AjaxResult getOrdersByStatus(@PathVariable("businessStatus") Integer businessStatus) {
        List<KanbanOrder> list = kanbanOrderService.selectKanbanOrderByStatus(businessStatus);
        return AjaxResult.success(list);
    }

    /**
     * 统计看板订单数量
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:list')")
    @GetMapping("/statistics")
    public AjaxResult getStatistics(@RequestParam Map<String, Object> params) {
        Map<String, Object> statistics = kanbanOrderService.selectKanbanOrderStatistics(params);
        return AjaxResult.success(statistics);
    }

    /**
     * 批量更新看板订单状态
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:edit')")
    @Log(title = "看板订单", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updateStatusBatch(@RequestParam("ids") Long[] ids, 
                                       @RequestParam("businessStatus") Integer businessStatus) {
        return toAjax(kanbanOrderService.updateKanbanOrderStatusBatch(
            Arrays.stream(ids).collect(Collectors.toList()), businessStatus));
    }

    /**
     * 发货方确认看板订单
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:confirm')")
    @Log(title = "看板订单", businessType = BusinessType.UPDATE)
    @PutMapping("/confirm/{id}")
    public AjaxResult confirmBySender(@PathVariable("id") Long id, 
                                     @RequestParam(value = "confirmRemark", required = false) String confirmRemark) {
        return toAjax(kanbanOrderService.confirmKanbanOrderBySender(id, confirmRemark));
    }

    /**
     * 打印箱标
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:print')")
    @Log(title = "看板订单", businessType = BusinessType.UPDATE)
    @PutMapping("/print/boxLabel/{id}")
    public AjaxResult printBoxLabel(@PathVariable("id") Long id) {
        return toAjax(kanbanOrderService.printBoxLabel(id));
    }

    /**
     * 打印看板
     */
    @PreAuthorize("@ss.hasPermi('kanban:order:print')")
    @Log(title = "看板订单", businessType = BusinessType.UPDATE)
    @PutMapping("/print/kanban/{id}")
    public AjaxResult printKanban(@PathVariable("id") Long id) {
        return toAjax(kanbanOrderService.printKanban(id));
    }
}